//
//  CategorySelectGridViewModel.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 16.04.25.
//

import Foundation
class CategorySelectGridViewModel : KoiAccountingBaseViewModel {
    
    private let categoryDao = KoiAccountingDB.standard.getCategoryDao()
    
    var disableCategoryMap: [String : AccountingCategory] = [String : AccountingCategory]()
    
    @Published var categoryList = [AccountingCategory]()
    
    func loadCategorys() {
        asyncBackground {
            var result = self.categoryDao.getCategoryByType(uid: self.uid, bookId: self.bookId, type: CategoryType.expense.rawValue)
            self.asyncMain {
                if !self.disableCategoryMap.isEmpty {
                    for (index, item) in result.enumerated() {
                        result[index].isDisable = self.disableCategoryMap.contains{ $0.key == item.cid }
                    }
                }
                self.categoryList = result
            }
        }
    }
    
    func initDisableCategoryMap(disableCategoryList: [AccountingCategory]?) {
        if let disableCategoryList = disableCategoryList {
            for item in disableCategoryList {
                if let cid = item.cid {
                    disableCategoryMap[cid] = item
                }
            }
        }
    }
}
